package com.sunchangming.stmdb.xgen;

public class TypeDouble extends Type {

	@Override
	public String getName(Variable variable) {
		return "double";
	}

	@Override
	public String getMarshalStr(Variable v, String osname) {
		return String.format("%s.writeDouble(%s)", osname, v.getName());
	}

	@Override
	public String getUnmarshalStr(Variable v, String osname, boolean useThis) {
		return String.format((useThis ? "this." : "") + "%s=%s.readDouble()", v.getName(), osname);
	}

	@Override
	public String getBoxName() {
		return "Double";
	}

	@Override
	public String getDefaultValue(Variable variable) {
		final String userstr = variable.defaultvalue;
		if (userstr != null && !userstr.isEmpty()) {
			try {
				return String.valueOf(Double.valueOf(userstr) + "d");
			} catch (final NumberFormatException ex) {
				throw new RuntimeException("错误的默认值" + userstr, ex);
			}
		}
		return "0d";
	}

	@Override
	public String getTestEqualsStr(Variable variable, String string) {
		return "this." + variable.getName() + "== " + string + "." + variable.getName();
	}

	@Override
	public String getHashCodeStr(Variable variable) {
		return String.format("(int)(Double.doubleToLongBits(this.%s)^(Double.doubleToLongBits(this.%s)>>32))",
				variable.getName(), variable.getName());
	}

}
